﻿package stuff3D.meshes
{
	import editeur.tools.RectangleSelector;
	import fab.geom.Line;
	import fl.controls.Button;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	import flash.utils.ByteArray;
	import stuff3D.sprites.MouseDot;
	
	/**
	 * ...
	 * @author fab
	 */
	public class InteractiveMesh extends Mesh
	{
		public var mouseDotArray:Vector.<MouseDot>;
		
		private var mouseDotContainer:Sprite;
		
		public function InteractiveMesh( _mouseDotContainer:Sprite, y_array:Array=null ):void 
		{
			super(  );
			
			mouseDotContainer = _mouseDotContainer;
			clean();
			setup();
		}
		public function clean():void 
		{
			mouseDotArray = new Vector.<MouseDot>();
		}
		private function setup():void 
		{
			//for ( var meshIndex:uint = 0; meshIndex < vertices.length; meshIndex++ ) mouseDotArray.push( mouseDotContainer.addChild( new MouseDot( meshIndex, vertices[ meshIndex ] ) ) );
		}
		public function move_selection( inc:Number ):void
		{
			if ( MouseDot.selection.length == 0 ) return;
			for each( var mDot:MouseDot in MouseDot.selection ) vertices[ mDot.meshIndex ].y -= inc;
		}
		public function selectionning():void
		{
			MouseDot.kill_selection();
			for each( var dot:MouseDot in mouseDotArray )
			{
				var dot_pt:Point = mouseDotContainer.localToGlobal( new Point( dot.x, dot.y ) );
				var intersect:Boolean = RectangleSelector.selection.containsPoint( dot_pt );
				if ( intersect ) dot.select();
				//else dot.deselect();
			}
		}
		public function get_rendered_dots( container:Sprite ):void
		{
			/*
			for (var i:int = 0; i < nb_dots_largeur; i++) 
			{
				for (var j:int = 0; j < nb_dots_hauteur; j++) 
				{
					var index:int = i * nb_dots_hauteur + j ;
					var dot:Vector3D = vertices[ index ];
					var pt:Point = container.local3DToGlobal( dot );
					
					var dot_ground:Vector3D = vertices_ground[ index ];
					var vc3Dproj:Vector3D = new Vector3D();
					vc3Dproj.x = dot_ground.x;
					vc3Dproj.y = 100;
					vc3Dproj.z = dot_ground.z;
			
					var pt1:Point = container.local3DToGlobal( vc3Dproj );
					var pt2:Point = container.local3DToGlobal( dot_ground );
					
					// interactive mouse dots
					mouseDotArray[ index ].x = pt.x;
					mouseDotArray[ index ].y = pt.y;					
					mouseDotArray[ index ].lineConstraint = new Line( pt1, pt2 );
				}
			}
			*/
		}
	}
}